package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.R;
import saucaca.CustomView.CustomExpandableListView;
import saucaca.Object.HomeData;
import saucaca.Object.RingtoneObject;
import saucaca.Util.TagName;
import saucaca.Util.UtilMusic;
import saucaca.fragment.RingToneListSong;
import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

@SuppressLint("InflateParams")
public class ExpandableRingtoneAdapter extends BaseExpandableListAdapter {
	private int[] groupStatus;
	ArrayList<RingtoneObject> listRingtoneObjects;
	CustomExpandableListView mExpandableListView;
	RingToneListSong ringToneListSong;
	Context mContext;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	RingtoneObject toneObject;
	Runnable onEverySecond;
	
	String numberViettel=null;
	String numberVina=null;
	String numberMobi=null;
	String numberVietnamMobi=null;
	String telcoViettel;
	String telcoVina;
	String telcoMobi;
	String telcoVietnamMobi;
	String cuphapTaiViettel="";
	String cuphapTangViettel="";
	String cuphapTaiVina="";
	String cuphapTangVina="";
	String cuphapTaiMobi="";
	String cuphapTangMobi="";
	String cuphapTaiVietnamMobi="";
	String cuphapTangVietnamMobi="";
	
	String masoViettel="1";
	String masoVina="1";
	String masoMobi="1";
	String masoVietnamMobi="1";
	
	@SuppressWarnings("deprecation")
	public ExpandableRingtoneAdapter(RingToneListSong ringToneListSong, CustomExpandableListView pExpandableListView, List<RingtoneObject> listRingtoneObjects) {
		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default)
				.cacheInMemory().cacheOnDisc().build();
		this.ringToneListSong = ringToneListSong;
		mContext = ringToneListSong.getActivity();
		this.mExpandableListView = pExpandableListView;
		mExpandableListView.setGroupIndicator(mContext.getResources().getDrawable(R.drawable.expandlist_indicator));
		this.listRingtoneObjects = (ArrayList<RingtoneObject>) listRingtoneObjects;
		groupStatus = new int[this.listRingtoneObjects.size()];
		ringToneListSong.mediaPlayer = new MediaPlayer();
		telcoViettel=mContext.getResources().getString(R.string.telco_viettel);
		telcoVina=mContext.getResources().getString(R.string.telco_vina);
		telcoMobi=mContext.getResources().getString(R.string.telco_mobi);
		telcoVietnamMobi=mContext.getResources().getString(R.string.telco_vietnammobi);
		setListEvent();
	}

	public void expandGroup(int groupPosition) {
		if (mExpandableListView.isGroupExpanded(groupPosition)) {
			mExpandableListView.collapseGroup(groupPosition);
		} else
			mExpandableListView.expandGroup(groupPosition);
	}

	private void setListEvent() {
		mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int arg0) {
				groupStatus[arg0] = 1;
				int len = listRingtoneObjects.size();
				for (int i = 0; i < len; i++) {
					if (i != arg0) {
						mExpandableListView.collapseGroup(i);
					}
				}
			}
		});
		mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int arg0) {
				groupStatus[arg0] = 0;
			}
		});
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int arg0) {
		return listRingtoneObjects.get(arg0);
	}

	@Override
	public int getGroupCount() {
		return listRingtoneObjects.size();
	}

	@Override
	public ChildHolder getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
		final GroupHolder groupHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_ringtone_group, null);
			groupHolder = new GroupHolder();
			groupHolder.realativeGroup = (RelativeLayout) convertView.findViewById(R.id.relativeGroup);
			groupHolder.maso = (TextView) convertView.findViewById(R.id.item_ringtone_group_Maso);
			groupHolder.luottai = (TextView) convertView.findViewById(R.id.item_ringtone_group_luottai);
			groupHolder.title = (TextView) convertView.findViewById(R.id.ringtone_group_item_title);
			groupHolder.gia = (TextView) convertView.findViewById(R.id.item_ringtone_group_gia);
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		groupHolder.realativeGroup.setOnClickListener(new View.OnClickListener() {
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				expandGroup(groupPosition);
			}
		});
		groupHolder.title.setText(listRingtoneObjects.get(groupPosition).getTXNAME() + " - " + listRingtoneObjects.get(groupPosition).getNAME_SINGERS());
		groupHolder.maso.setText(listRingtoneObjects.get(groupPosition).getSONG_ID());
		groupHolder.luottai.setText(listRingtoneObjects.get(groupPosition).getISDOWN());
		// groupHolder.gia.setText(listRingtoneObjects.get(groupPosition).get);
		return convertView;
	}

	int mediaFileLengthInMilliseconds = 0;

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		final ChildHolder holder = new ChildHolder();
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_ringtone_child, null);
		}
		holder.buttonSetringtone = (LinearLayout) convertView.findViewById(R.id.item_ringtone_child_button_cainhaccho);
		holder.buttonGift = (LinearLayout) convertView.findViewById(R.id.item_ringtone_child_button_tang);
		holder.buttonPlay = (ImageView) convertView.findViewById(R.id.item_ringtone_child_playpause);
		holder.txtCurrent = (TextView) convertView.findViewById(R.id.item_ringtone_child_textbufferStart1);
		holder.txtLenght = (TextView) convertView.findViewById(R.id.item_ringtone_child_textbufferEnd1);
		holder.seekBarProgress = (SeekBar) convertView.findViewById(R.id.item_ringtone_child_seekBars1);
		holder.viettel = (TextView) convertView.findViewById(R.id.ringtone_telco_viettel);
		holder.vinaphone = (TextView) convertView.findViewById(R.id.ringtone_telco_vinaphone);
		holder.vietnammobi = (TextView) convertView.findViewById(R.id.ringtone_telco_viennammobi);
		holder.mobifone = (TextView) convertView.findViewById(R.id.ringtone_telco_mobifone);
		
		holder.content=(TextView) convertView.findViewById(R.id.item_ringtone_child_sms);
		
		holder.viettel_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_viettel_tongdai);
		holder.vinaphone_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_vinaphone_tongdai);
		holder.vietnammobi_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_viennammobi_tongdai);
		holder.mobifone_tongdai = (TextView) convertView.findViewById(R.id.ringtone_telco_mobifone_tongdai);
		
		holder.buttonViettel = (LinearLayout) convertView.findViewById(R.id.ringtone_viettel);
		holder.buttonVina = (LinearLayout) convertView.findViewById(R.id.ringtone_vinaphone);
		holder.buttonMobi = (LinearLayout) convertView.findViewById(R.id.ringtone_mobifone);
		holder.buttonVietNamm = (LinearLayout) convertView.findViewById(R.id.ringtone_vinammobile);
		
		holder.buttonPlay.setImageResource(R.drawable.nc_pause);
		try {
			ringToneListSong.mediaPlayer.stop();
			ringToneListSong.mediaPlayer.reset();
			ringToneListSong.mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			ringToneListSong.mediaPlayer.setDataSource(listRingtoneObjects.get(groupPosition).getPATCH_DEMO());
			ringToneListSong.mediaPlayer.prepareAsync();
		} catch (Exception e) {
			e.printStackTrace();
		}

		ringToneListSong.mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				mediaFileLengthInMilliseconds = ringToneListSong.mediaPlayer.getDuration();
				holder.txtLenght.setText(UtilMusic.milliSecondsToTimer(mediaFileLengthInMilliseconds));
			}
		});

		holder.seekBarProgress.setMax(99);
		holder.seekBarProgress.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
					ringToneListSong.mediaPlayer.seekTo(holder.seekBarProgress.getProgress());
				return false;
			}
		});
		for (int i = 0; i < HomeData.homeListTelco.size(); i++) {
			if(HomeData.homeListTelco.get(i).getCODE().equals(telcoViettel)){
				numberViettel=HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiViettel=HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangViettel=HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
			if(HomeData.homeListTelco.get(i).getCODE().equals(telcoVina)){
				numberVina=HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiVina=HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangVina=HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
			if(HomeData.homeListTelco.get(i).getCODE().equals(telcoMobi)){
				numberMobi=HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiMobi=HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangMobi=HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
			if(HomeData.homeListTelco.get(i).getCODE().equals(telcoVietnamMobi)){
				numberVietnamMobi=HomeData.homeListTelco.get(i).getSHORT_CODE();
				cuphapTaiVietnamMobi=HomeData.homeListTelco.get(i).getSYNTAX();
				cuphapTangVietnamMobi=HomeData.homeListTelco.get(i).getSEND_GIFTS();
			}
		}
		
		holder.viettel_tongdai.setText(numberViettel);
		holder.vinaphone_tongdai.setText(numberVina);
		holder.mobifone_tongdai.setText(numberMobi);
		holder.vietnammobi_tongdai.setText(numberVietnamMobi);
		
		holder.viettel.setText(listRingtoneObjects.get(groupPosition).getVTL_SYNTAX());
		holder.vinaphone.setText(listRingtoneObjects.get(groupPosition).getGPC_SYNTAX());
		holder.mobifone.setText(listRingtoneObjects.get(groupPosition).getVMS_SYNTAX());
		holder.vietnammobi.setText(listRingtoneObjects.get(groupPosition).getVNM_SYNTAX());
		
		holder.buttonSetringtone.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TagName.showToastShort(mContext, "Cài nhạc chờ");
			}
		});

		holder.buttonGift.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TagName.showToastShort(mContext, "Tặng nhạc chờ");
			}
		});
	
		if(listRingtoneObjects.get(groupPosition).getVTL_SYNTAX()!=null &&listRingtoneObjects.get(groupPosition).getVTL_SYNTAX().length()>0){
			masoViettel=listRingtoneObjects.get(groupPosition).getVTL_SYNTAX();
			if(!masoViettel.equals("null"))
			holder.buttonViettel.setVisibility(View.VISIBLE);
		}
			
		if(listRingtoneObjects.get(groupPosition).getGPC_SYNTAX()!=null&&listRingtoneObjects.get(groupPosition).getGPC_SYNTAX().length()>0){
			masoVina=listRingtoneObjects.get(groupPosition).getGPC_SYNTAX();
			if(!masoVina.equals("null"))
				holder.buttonVina.setVisibility(View.VISIBLE);
		}
		
		if(listRingtoneObjects.get(groupPosition).getVMS_SYNTAX()!=null&&listRingtoneObjects.get(groupPosition).getVMS_SYNTAX().length()>0){
			masoMobi=listRingtoneObjects.get(groupPosition).getVMS_SYNTAX();
			if(!masoMobi.equals("null"))
				holder.buttonMobi.setVisibility(View.VISIBLE);
		}
		
		if(listRingtoneObjects.get(groupPosition).getVNM_SYNTAX()!=null&&listRingtoneObjects.get(groupPosition).getVNM_SYNTAX().length()>0){
			masoVietnamMobi=listRingtoneObjects.get(groupPosition).getVNM_SYNTAX();
			if(!masoVietnamMobi.equals("null"))
				holder.buttonVietNamm.setVisibility(View.VISIBLE);
		}
		
		
		holder.buttonViettel.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			BaseEngine.installRingtone(mContext, masoViettel,cuphapTaiViettel,cuphapTangViettel, numberViettel);
		}
		});
		holder.buttonVina.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				BaseEngine.installRingtone(mContext, masoVina,cuphapTaiVina,cuphapTangVina, numberVina);
			}
			});
		holder.buttonMobi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				BaseEngine.installRingtone(mContext, masoMobi,cuphapTaiMobi,cuphapTangMobi, numberMobi);
			}
			});
		holder.buttonVietNamm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				BaseEngine.installRingtone(mContext, masoVietnamMobi,cuphapTaiVietnamMobi,cuphapTangVietnamMobi, numberVietnamMobi);
			}
			});
		holder.buttonPlay.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!ringToneListSong.mediaPlayer.isPlaying()) {
					ringToneListSong.mediaPlayer.start();
					holder.buttonPlay.setImageResource(R.drawable.nc_play);
					holder.seekBarProgress.postDelayed(onEverySecond, 1000);
				} else {
					holder.buttonPlay.setImageResource(R.drawable.nc_pause);
					ringToneListSong.mediaPlayer.pause();
				}
			}
		});
		onEverySecond = new Runnable() {
			@Override
			public void run() {
				if(ringToneListSong.mediaPlayer!=null)
				if (ringToneListSong.mediaPlayer.isPlaying()) {
					if (holder.seekBarProgress != null) {
						holder.seekBarProgress.setProgress((int) (((float) ringToneListSong.mediaPlayer.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100));
					}
					if (ringToneListSong.mediaPlayer.isPlaying()) {
						holder.seekBarProgress.postDelayed(onEverySecond, 1000);
						holder.txtCurrent.setText(UtilMusic.milliSecondsToTimer(ringToneListSong.mediaPlayer.getCurrentPosition()));
					}
				}
			}
		};
		ringToneListSong.mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
			@Override
			public void onBufferingUpdate(MediaPlayer mp, int percent) {
				holder.seekBarProgress.setSecondaryProgress(percent);
			}
		});
		ringToneListSong.mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mp) {
				holder.buttonPlay.setImageResource(R.drawable.nc_pause);
				holder.seekBarProgress.setProgress(0);
				holder.txtCurrent.setText(UtilMusic.milliSecondsToTimer(0));
			}
		});
		
	
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	class GroupHolder {
		boolean isExpanded = false;
		RelativeLayout realativeGroup;
		TextView title;
		TextView maso;
		TextView luottai;
		TextView gia;
	}

	class ChildHolder {
		ImageView buttonPlaypause;
		LinearLayout buttonSetringtone;
		LinearLayout buttonGift;
		LinearLayout buttonViettel;
		LinearLayout buttonVina;
		LinearLayout buttonMobi;
		LinearLayout buttonVietNamm;

		ImageView buttonPlay;
		TextView txtCurrent;
		TextView txtLenght;
		SeekBar seekBarProgress;
		TextView content;
		TextView viettel;
		TextView vinaphone;
		TextView mobifone;
		TextView vietnammobi;
		TextView viettel_tongdai;
		TextView vinaphone_tongdai;
		TextView mobifone_tongdai;
		TextView vietnammobi_tongdai;
	}
}
